﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit;
using HYView;
using QuickModel.CommonTrans;
using QuickModel.Forms;
using QuickModel.InteractiveOperation;

namespace QuickModel.AxisTransModel
{
	public class AxisModelRequestMaker : InteractiveOperationBase
	{
		public override bool IfBreak
		{
			get
			{
				return true;
			}
		}

		public override void CloseFrom()
		{
		}

		public AxisModelRequestMaker(ExternalCommandData useExternalCommandData) : base(useExternalCommandData)
		{
			this.m_doc = useExternalCommandData.Application.ActiveUIDocument.Document;
			this.m_lstRequest = new List<InputRequest>();
			this.m_lstAxisLineInfo = new List<CurveInfo>();
			this.m_lstAxisNumberInfo = new List<ICadElement>();
			this.m_lstAxisLineCategory = new List<Category>();
			this.m_lstAxisNumberCategory = new List<Category>();
		}

		public override List<InputRequest> GetAllInputRequest()
		{
			return this.m_lstRequest;
		}

		public override void PrepareRequest()
		{
			CmdViewType types = (CmdViewType)13;
			bool flag = SwitchView.SetUsableView(types, this.m_useExternalCommandData) > 0;
			if (flag)
			{
				this.m_bCancel = true;
			}
			else
			{
				List<string> docAxisTypes = AxisManager.GetDocAxisTypes(this.m_doc);
				bool flag2 = docAxisTypes == null || docAxisTypes.Count < 1;
				if (flag2)
				{
					YJKMessageBox.Show("当前的Revit没有加载轴网类型，不能进行轴网翻模！");
					this.m_bCancel = true;
				}
				else
				{
					this.m_bCancel = false;
					AxisModelTransForm axisModelTransForm = new AxisModelTransForm(this, docAxisTypes);
					for (;;)
					{
						bool flag3 = axisModelTransForm.ShowDialog() != DialogResult.OK;
						if (flag3)
						{
							break;
						}
						List<string> list = new List<string>();
						List<Category> list2 = new List<Category>();
						bool flag4 = axisModelTransForm.DlgCloseType == AxisModelTransForm.CloseDlgType.RT_SelectAxisLine;
						if (flag4)
						{
							List<CurveInfo> list3 = this.SelectAxisLine("请选择链接dwg图中的轴线图层", list, list2);
							bool flag5 = list3 != null && list3.Count > 0;
							if (flag5)
							{
								this.m_lstAxisLineInfo.AddRange(list3);
								for (int i = 0; i < list.Count; i++)
								{
									bool flag6 = axisModelTransForm.AddAxisTypeLayer(list[i]);
									if (flag6)
									{
										this.m_lstAxisLineCategory.Add(list2[i]);
									}
								}
							}
						}
						else
						{
							bool flag7 = axisModelTransForm.DlgCloseType == AxisModelTransForm.CloseDlgType.RT_SelectAxisNumber;
							if (!flag7)
							{
								goto IL_1D9;
							}
							List<ICadElement> list4 = this.SelectAxisNumber("请选择链接dwg图中的轴号及轴号圈图层", list, list2);
							bool flag8 = list4 != null && list4.Count > 0;
							if (flag8)
							{
								this.m_lstAxisNumberInfo.AddRange(list4);
								for (int j = 0; j < list.Count; j++)
								{
									bool flag9 = axisModelTransForm.AddAxisNumberLayer(list[j]);
									if (flag9)
									{
										this.m_lstAxisNumberCategory.Add(list2[j]);
									}
								}
							}
						}
					}
					this.m_bCancel = true;
					this.ShowSelectLayeies();
					return;
					IL_1D9:
					bool flag10 = axisModelTransForm.DlgCloseType == AxisModelTransForm.CloseDlgType.RT_TransForLevel;
					if (flag10)
					{
						this.ShowSelectLayeies();
						this.AxisTransForLevel(axisModelTransForm.AxisLineLayers, axisModelTransForm.AxisNumberLayers, axisModelTransForm.SelectGridType);
					}
					else
					{
						bool flag11 = axisModelTransForm.DlgCloseType == AxisModelTransForm.CloseDlgType.RT_TransForLocal;
						if (flag11)
						{
							this.ShowSelectLayeies();
							this.AxisTransForLocal(axisModelTransForm.AxisLineLayers, axisModelTransForm.AxisNumberLayers, axisModelTransForm.SelectGridType);
						}
						else
						{
							this.m_bCancel = true;
							this.ShowSelectLayeies();
						}
					}
				}
			}
		}

		public void ClearSelectedAxisLine()
		{
			Transaction transaction = new Transaction(this.m_doc);
			transaction.Start("ShowLayer");
			try
			{
				foreach (Category category in this.m_lstAxisLineCategory)
				{
					this.m_doc.ActiveView.HYSetVisibility(category, true);
				}
			}
			catch
			{
			}
			transaction.Commit();
			this.m_lstAxisLineInfo.Clear();
			this.m_lstAxisLineCategory.Clear();
		}

		public void ClearSelectedAxisNumber()
		{
			Transaction transaction = new Transaction(this.m_doc);
			transaction.Start("ShowLayer");
			try
			{
				foreach (Category category in this.m_lstAxisNumberCategory)
				{
					this.m_doc.ActiveView.HYSetVisibility(category, true);
				}
			}
			catch
			{
			}
			transaction.Commit();
			this.m_lstAxisNumberInfo.Clear();
			this.m_lstAxisNumberCategory.Clear();
		}

		private void ShowSelectLayeies()
		{
			Transaction transaction = new Transaction(this.m_doc);
			transaction.Start("ShowLayer");
			try
			{
				foreach (Category category in this.m_lstAxisLineCategory)
				{
					this.m_doc.ActiveView.HYSetVisibility(category, true);
				}
				foreach (Category category2 in this.m_lstAxisNumberCategory)
				{
					this.m_doc.ActiveView.HYSetVisibility(category2, true);
				}
			}
			catch
			{
			}
			transaction.Commit();
		}

		private void AxisTransForLevel(List<string> lstAxisLineLayer, List<string> lstAxisNumberLayer, string strAxisType)
		{
			List<ICadElement> list = new List<ICadElement>();
			list.AddRange(this.m_lstAxisLineInfo);
			list.AddRange(this.m_lstAxisNumberInfo);
			this.AxisTrans(list, lstAxisLineLayer, lstAxisNumberLayer, strAxisType);
		}

		private void AxisTransForLocal(List<string> lstAxisLineLayer, List<string> lstAxisNumberLayer, string strAxisType)
		{
			List<ICadElement> list = new List<ICadElement>();
			list.AddRange(this.m_lstAxisLineInfo);
			list.AddRange(this.m_lstAxisNumberInfo);
			List<ICadElement> list2 = new List<ICadElement>();
			DelegateInRange<ICadElement> delegateInRange = new DelegateInRange<ICadElement>(RevitCommonFunctions.IsElementInBox);
			try
			{
				while (list2.Count < 1)
				{
					this.m_dwgEntitiesSelection.TryGetEntitesInPickBox<ICadElement>(out list2, list, delegateInRange, "请框选识别范围");
				}
			}
			catch
			{
				this.m_bCancel = true;
				return;
			}
			this.AxisTrans(list2, lstAxisLineLayer, lstAxisNumberLayer, strAxisType);
		}

		private void AxisTrans(List<ICadElement> lstCadElemInfo, List<string> lstAxisLineLayer, List<string> lstAxisNumberLayer, string strAxisType)
		{
			bool flag = lstCadElemInfo == null || lstCadElemInfo.Count < 1;
			if (flag)
			{
				YJKMessageBox.Show("没有选择要翻模的图元！");
				this.m_bCancel = true;
			}
			else
			{
				GridType gridTypeByName = AssistFunc.GetGridTypeByName(this.m_doc, strAxisType);
				bool flag2 = gridTypeByName == null;
				if (flag2)
				{
					YJKMessageBox.Show("没有选择轴网类型！");
					this.m_bCancel = true;
				}
				else
				{
					this.ShowGridTypeNumber(gridTypeByName);
					List<Curve> axisCurves = AxisManager.GetAxisCurves(lstCadElemInfo, lstAxisLineLayer);
					List<Curve> axisNumberCurves = AxisManager.GetAxisNumberCurves(lstCadElemInfo, lstAxisNumberLayer);
					List<TextInfo> axisNumberTextes = AxisManager.GetAxisNumberTextes(lstCadElemInfo, lstAxisNumberLayer);
					List<AxisNumber> axisNumber = AxisManager.GetAxisNumber(axisNumberCurves, axisNumberTextes);
					AxisManager.MergeOverlapCurves(axisNumberCurves);
					AxisManager.MergeAxisCurves(false, axisCurves);
					AxisManager.ResetExtendCurvesPoint(axisCurves, axisNumberCurves);
					AxisManager.MergeExtendToAxisCurves(axisCurves, axisNumberCurves);
					this.CreateAxisRequest(gridTypeByName, axisCurves, axisNumber, axisNumberCurves);
				}
			}
		}

		private List<CurveInfo> SelectAxisLine(string strPrompt, List<string> lstLayerName, List<Category> lstLayerCategory)
		{
			SelecteEntType type = SelecteEntType.eLine | SelecteEntType.eArc | SelecteEntType.ePolyLine;
			List<CurveInfo> list = new List<CurveInfo>();
			Transaction transaction = new Transaction(this.m_doc);
			transaction.Start("HideLayer");
			for (;;)
			{
				try
				{
					List<CurveInfo> list2 = new List<CurveInfo>();
					bool flag = !this.m_dwgEntitiesSelection.TryPickSameLayerAllCurve(out list2, true, type, strPrompt);
					if (flag)
					{
						bool flag2 = false;
						string resultInfo = DwgEntitiesSelectionCommon.GetResultInfo(this.m_dwgEntitiesSelection.Result, out flag2);
						bool flag3 = !flag2;
						if (flag3)
						{
							YJKMessageBox.Show(resultInfo);
							transaction.RollBack();
						}
						break;
					}
					bool flag4 = list2 != null && list2.Count > 0;
					if (flag4)
					{
						string empty = string.Empty;
						Category item = null;
						bool cadElementLayer = RevitCommonFunctions.GetCadElementLayer(list2[0], ref empty, ref item);
						if (cadElementLayer)
						{
							list.AddRange(list2);
							lstLayerName.Add(empty);
							lstLayerCategory.Add(item);
						}
					}
				}
				catch
				{
					break;
				}
			}
			bool flag5 = !transaction.HasEnded();
			if (flag5)
			{
				transaction.Commit();
			}
			return list;
		}

		private List<ICadElement> SelectAxisNumber(string strPrompt, List<string> lstLayerName, List<Category> lstLayerCategory)
		{
			List<ICadElement> list = new List<ICadElement>();
			Transaction transaction = new Transaction(this.m_doc);
			transaction.Start("HideLayer");
			for (;;)
			{
				try
				{
					List<ICadElement> list2 = new List<ICadElement>();
					bool flag = !this.m_dwgEntitiesSelection.TryPickAxisNumber(out list2, true, strPrompt);
					if (flag)
					{
						bool flag2 = false;
						string resultInfo = DwgEntitiesSelectionCommon.GetResultInfo(this.m_dwgEntitiesSelection.Result, out flag2);
						bool flag3 = !flag2;
						if (flag3)
						{
							YJKMessageBox.Show(resultInfo);
							transaction.RollBack();
						}
						break;
					}
					bool flag4 = list2 != null && list2.Count > 0;
					if (flag4)
					{
						string empty = string.Empty;
						Category item = null;
						bool cadElementLayer = RevitCommonFunctions.GetCadElementLayer(list2[0], ref empty, ref item);
						if (cadElementLayer)
						{
							list.AddRange(list2);
							lstLayerName.Add(empty);
							lstLayerCategory.Add(item);
						}
					}
				}
				catch
				{
					break;
				}
			}
			bool flag5 = !transaction.HasEnded();
			if (flag5)
			{
				transaction.Commit();
			}
			return list;
		}

		private void ShowGridTypeNumber(GridType gt)
		{
			Transaction transaction = new Transaction(this.m_doc);
			transaction.Start("SetGridType");
			try
			{
				Parameter parameter = gt.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_1);
				bool flag = parameter != null;
				if (flag)
				{
					parameter.Set(1);
				}
				Parameter parameter2 = gt.get_Parameter(BuiltInParameter.GRID_BUBBLE_END_2);
				bool flag2 = parameter2 != null;
				if (flag2)
				{
					parameter2.Set(1);
				}
			}
			catch
			{
			}
			transaction.Commit();
		}

		private void CreateAxisRequest(GridType axisType, List<Curve> lstAxisCurve, List<AxisNumber> lstAxisNumber, List<Curve> lstExtendCurve)
		{
			List<string> docAxisNumbers = AxisManager.GetDocAxisNumbers(this.m_doc);
			List<List<Curve>> list = new List<List<Curve>>();
			List<List<Curve>> list2 = new List<List<Curve>>();
			for (int i = 0; i < lstAxisCurve.Count; i++)
			{
				List<Curve> axisCurveLoop = AxisManager.GetAxisCurveLoop(lstAxisCurve[i], lstExtendCurve);
				AxisNumber linkAxisNumber = AxisManager.GetLinkAxisNumber(axisCurveLoop[0], 0, lstAxisNumber);
				AxisNumber linkAxisNumber2 = AxisManager.GetLinkAxisNumber(axisCurveLoop[axisCurveLoop.Count - 1], 1, lstAxisNumber);
				bool flag = linkAxisNumber != null || linkAxisNumber2 != null;
				if (flag)
				{
					string strNumber = (linkAxisNumber == null) ? linkAxisNumber2.Number : linkAxisNumber.Number;
					AxisRequest axisRequest = new AxisRequest();
					axisRequest.Number = AxisManager.GetAxisNumber(strNumber, docAxisNumbers);
					axisRequest.AxisType = axisType;
					bool flag2 = linkAxisNumber == null;
					if (flag2)
					{
						axisRequest.HideNumber = 1;
					}
					else
					{
						bool flag3 = linkAxisNumber2 == null;
						if (flag3)
						{
							axisRequest.HideNumber = 0;
						}
						else
						{
							axisRequest.HideNumber = -1;
						}
					}
					axisRequest.AxisCurves = axisCurveLoop;
					this.m_lstRequest.Add(axisRequest);
					bool flag4 = linkAxisNumber != null;
					if (flag4)
					{
						lstAxisNumber.Remove(linkAxisNumber);
					}
					bool flag5 = linkAxisNumber2 != null;
					if (flag5)
					{
						lstAxisNumber.Remove(linkAxisNumber2);
					}
				}
				else
				{
					bool flag6 = this.IsHoriAxisCurve(axisCurveLoop);
					if (flag6)
					{
						list.Add(axisCurveLoop);
					}
					else
					{
						list2.Add(axisCurveLoop);
					}
				}
			}
			this.CreateHoriAxisRequest(axisType, docAxisNumbers, list);
			this.CreateVertAxisRequest(axisType, docAxisNumbers, list2);
		}

		private void CreateVertAxisRequest(GridType axisType, List<string> lstAllAxisNumber, List<List<Curve>> lstAxisCurveVert)
		{
			this.SortCurves(true, lstAxisCurveVert);
			foreach (List<Curve> axisCurves in lstAxisCurveVert)
			{
				string text = AxisManager.GetMaxNumber(lstAllAxisNumber);
				bool flag = string.IsNullOrWhiteSpace(text);
				if (flag)
				{
					text = "1";
				}
				else
				{
					int num = Convert.ToInt32(text);
					text = (num + 1).ToString();
				}
				AxisRequest axisRequest = new AxisRequest();
				axisRequest.Number = AxisManager.GetAxisNumber(text, lstAllAxisNumber);
				axisRequest.AxisType = axisType;
				axisRequest.HideNumber = 2;
				axisRequest.AxisCurves = axisCurves;
				this.m_lstRequest.Add(axisRequest);
			}
		}

		private void CreateHoriAxisRequest(GridType axisType, List<string> lstAllAxisNumber, List<List<Curve>> lstAxisCurveHori)
		{
			this.SortCurves(false, lstAxisCurveHori);
			foreach (List<Curve> axisCurves in lstAxisCurveHori)
			{
				string text = AxisManager.GetMaxAlpha(lstAllAxisNumber);
				bool flag = string.IsNullOrWhiteSpace(text);
				if (flag)
				{
					text = "A";
				}
				else
				{
					int num = AxisManager.AlphaToInt(text);
					text = AxisManager.IntToAlpha(num + 1);
				}
				AxisRequest axisRequest = new AxisRequest();
				axisRequest.Number = AxisManager.GetAxisNumber(text, lstAllAxisNumber);
				axisRequest.AxisType = axisType;
				axisRequest.HideNumber = 2;
				axisRequest.AxisCurves = axisCurves;
				this.m_lstRequest.Add(axisRequest);
			}
		}

		private bool IsHoriAxisCurve(List<Curve> lstAxisCurve)
		{
			bool result = true;
			double num = 0.0;
			foreach (Curve curve in lstAxisCurve)
			{
				XYZ endPoint = curve.GetEndPoint(0);
				XYZ endPoint2 = curve.GetEndPoint(1);
				double num2 = Math.Abs(endPoint.X - endPoint2.X);
				double num3 = Math.Abs(endPoint.Y - endPoint2.Y);
				bool flag = num2 > num3;
				bool flag2 = num < curve.Length;
				if (flag2)
				{
					result = flag;
					num = curve.Length;
				}
			}
			return result;
		}

		private void SortCurves(bool bSortByX, List<List<Curve>> lstAxisCurves)
		{
			for (int i = 0; i < lstAxisCurves.Count - 1; i++)
			{
				List<Curve> list = lstAxisCurves[i];
				XYZ xyz = list[0].GetEndPoint(0);
				for (int j = i + 1; j < lstAxisCurves.Count; j++)
				{
					List<Curve> list2 = lstAxisCurves[j];
					XYZ endPoint = list2[0].GetEndPoint(0);
					int num = bSortByX ? xyz.X.CompareTo(endPoint.X) : xyz.Y.CompareTo(endPoint.Y);
					bool flag = num > 0;
					if (flag)
					{
						List<Curve> value = lstAxisCurves[i];
						lstAxisCurves[i] = lstAxisCurves[j];
						lstAxisCurves[j] = value;
						xyz = endPoint;
					}
				}
			}
		}

		private Document m_doc;

		private List<InputRequest> m_lstRequest;

		private List<CurveInfo> m_lstAxisLineInfo;

		private List<ICadElement> m_lstAxisNumberInfo;

		private List<Category> m_lstAxisLineCategory;

		private List<Category> m_lstAxisNumberCategory;
	}
}
